﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Caliburn.Micro;
using Phone.Framework.Config;
using System.Collections.Generic;
using Phone.Framework.CaliburnExtensions;

namespace CetWin.ViewModels
{
    public class SettingsViewModel : Screen
    {
        public SettingsViewModel(CetWinSetting set)
        {
            Setting = set;
            this.DisplayName = "Settings";
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
        }

        public CetWinSetting Setting { get; set; }

        protected override void OnDeactivate(bool close)
        {
            if (IoC.Get<CetWinSetting>().IsEdited)
                IoC.Get<IEventAggregator>().Publish(new HandleMessage(Messages.UpdateSetting));
            IoC.Get<CetWinSetting>().IsEdited = false;
            base.OnDeactivate(close);
        }
    }

    public class CetWinSetting : PropertyChangedBase
    {
        private SettingManager _settingManager;
        public CetWinSetting(SettingManager settingManager)
        {
            _settingManager = settingManager;
        }

        public bool IsEdited { get; set; }

        public int SearchResultCount
        {
            get
            {
                return _settingManager.Get<int>("SearchResultCount", 10);
            }
            set
            {
                _settingManager.Set<int>("SearchResultCount", value);
                NotifyOfPropertyChange("SearchResultCount");
                IsEdited = true;
            }
        }

        public string SearchPriority
        {
            get
            {
                return _settingManager.Get<string>("SearchPriority", "Normal");
            }
            set
            {
                _settingManager.Set<string>("SearchPriority", value);
                NotifyOfPropertyChange("SearchPriority");
                IsEdited = true;
            }
        }

        public string WordItemTemplate
        {
            get { return _settingManager.Get<string>("WordItemTemplate", "Normal"); }
            set
            {
                _settingManager.Set<string>("WordItemTemplate", value);
                NotifyOfPropertyChange("WordItemTemplate");
                IsEdited = true;
            }
        }

        public bool IsCaseSensitive
        {
            get { return _settingManager.Get<bool>("IsCaseSensitive", false); }
            set
            {
                _settingManager.Set<bool>("IsCaseSensitive", value);
                NotifyOfPropertyChange("IsCaseSensitive");
                IsEdited = true;
            }
        }

        public bool AutoNetworkSearch
        {
            get { return _settingManager.Get<bool>("AutoNetworkSearch", true); }
            set
            {
                _settingManager.Set<bool>("AutoNetworkSearch", value);
                NotifyOfPropertyChange("AutoNetworkSearch");
                if (!value)
                    AutoSpellWord = value;
            }
        }

        public bool AutoSpellWord
        {
            get { return _settingManager.Get<bool>("AutoSpellWord", false); }
            set
            {
                _settingManager.Set<bool>("AutoSpellWord", value);
                NotifyOfPropertyChange("AutoSpellWord");
                if (value)
                    AutoNetworkSearch = true;
            }
        }

        public string SearchEngine
        {
            get { return _settingManager.Get<string>("SearchEngine", "youdao"); }
            set
            {
                _settingManager.Set<string>("SearchEngine", value);
                NotifyOfPropertyChange("SearchEngine");
                IsEdited = true;
            }
        }

        public bool EnableRemindNotification
        {
            get { return _settingManager.Get<bool>("EnableRemindNotification",false); }
            set
            {
                _settingManager.Set<bool>("EnableRemindNotification", value);
                NotifyOfPropertyChange("EnableRemindNotification");
                IsEdited = true;
            }
        }

        public DateTime RemindTime
        {
            get { return _settingManager.Get<DateTime>("RemindTime", DateTime.Now); }
            set
            {
                _settingManager.Set<DateTime>("RemindTime", value);
                NotifyOfPropertyChange("RemindTime");
                IsEdited = true;
            }
        }

        public string SelectedWordLibrary
        {
            get { return _settingManager.Get<string>("SelectedWordLibrary", "cet4"); }
            set
            {
                _settingManager.Set<string>("SelectedWordLibrary", value);
                NotifyOfPropertyChange("SelectedWordLibrary");
                IsEdited = true;
            }
        }

        public bool ShowWarnWhenExit
        {
            get { return _settingManager.Get<bool>("ShowWarnWhenExit", true); }
            set
            {
                _settingManager.Set<bool>("ShowWarnWhenExit", value);
                NotifyOfPropertyChange("ShowWarnWhenExit");
                IsEdited = true;
            }
        }
    }
}